//
//  ThreeImageCell.m
//  WeiMo
//
//  Created by weixian he on 12-8-18.
//  Copyright (c) 2012年 . All rights reserved.
//

#import "ThreeImageCell.h"
#import <QuartzCore/QuartzCore.h>
#import "DLog.h"


#define kFourImageNums 4.0
#define kFourImageIntNums 4
#define kImageNums 3.0
#define kImageIntNums 3
#define kSpaceX 2.0
//#define kSpaceX 1.5
#define kSpaceX2 1.0
#define kSpaceY 3.0


@implementation ThreeImageCell
@synthesize imageViews;
@synthesize checkView;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier canCheck:(BOOL) canCheck
{
    DEnter();
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //self.userInteractionEnabled = NO; 
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.imageViews = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
        CGRect rect = self.frame;
        //float width = (rect.size.width - (kSpaceX *2 *kImageNums)) / kImageNums;
        float width = (rect.size.width - kSpaceX * (kFourImageIntNums + 1)) / kFourImageNums;
        float height = width;
        float x = rect.origin.x;
        for(int i = 1; i<=kFourImageIntNums; i++)
        {
            //float x1 = kSpaceX * (i*2 -1) + (width + x) *(i -1);
            float x1;
            if(i == 1)
            {
                x1 = kSpaceX * (i*2 -1) + (width + x) *(i -1);
            }
            else {
                x1 = kSpaceX2 + kSpaceX2 * (i*2 -1) + (width + x) *(i -1);
            }
            
            CGRect rect1 = CGRectMake(x1, kSpaceY, width, height);
            TouchImageView *imageView = [[[TouchImageView alloc] initWithFrame:rect1] autorelease];
            if(canCheck)
            {
                //imageView viewWithTag:<#(NSInteger)#>
                UIImageView *checkViewD = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"check.png"]] autorelease];
                self.checkView .alpha = 0.8;
                CGRect rect = CGRectMake(80 - 24, 2, 21, 21);
                checkViewD.frame = rect;
                checkViewD.tag = kCanCheckTag;
                //checkView.hidden = YES;
                [imageView addSubview:checkViewD];

            }
            //imageView.layer.masksToBounds = YES;
            //imageView.layer.cornerRadius = 6.0;
            //imageView.layer.borderWidth = 1.0;
            imageView.layer.borderColor = [[UIColor grayColor] CGColor];
            imageView.tag = i;
            [imageViews addObject:imageView];
            [self addSubview:imageView];
        }
        self.contentView.backgroundColor = [UIColor colorWithHue:26.0/255 saturation:26.0/255 brightness:26.0/255 alpha:1.0];
        //self.contentView.backgroundColor = [UIColor clearColor];
    }
    return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    DEnter();
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //self.userInteractionEnabled = NO; 
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.imageViews = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
        CGRect rect = self.frame;
        float width = (rect.size.width - (kSpaceX * 4)) / kImageNums;
        //float width = (rect.size.width - kSpaceX *2 - (kSpaceX2*2 * (kImageIntNums -1))) / kImageNums;
        //float width = 78.0;
        float height = width;
        float x = rect.origin.x;
        for(int i = 1; i<=kImageIntNums; i++)
        {
            //float x1 = kSpaceX * (i*2 -1) + (width + x) *(i -1);
            float x1;
            if(i == 1)
            {
                x1 = kSpaceX * (i*2 -1) + (width + x) *(i -1);
            }
            else {
                x1 = kSpaceX2 + kSpaceX2 * (i*2 -1) + (width + x) *(i -1);
            }
            CGRect rect1 = CGRectMake(x1, kSpaceY, width, height);
            TouchImageView *imageView = [[[TouchImageView alloc] initWithFrame:rect1] autorelease];
            //长按
            //[imageView initialize];
            
            //imageView.layer.masksToBounds = YES;
            //imageView.layer.cornerRadius = 6.0;
            //imageView.layer.borderWidth = 1.0;
            imageView.layer.borderColor = [[UIColor grayColor] CGColor];
            imageView.tag = i;
            [imageViews addObject:imageView];
            [self addSubview:imageView];
        }
        //self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor colorWithHue:26.0/255 saturation:26.0/255 brightness:26.0/255 alpha:1.0];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)dealloc
{
    DEnter();
    self.imageViews = nil;
    self.checkView = nil;
    [super dealloc];
}
@end
